package com.liam.friend.service.exam.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liam.common.core.constants.Constants;
import com.liam.common.core.domain.TableDataInfo;
import com.liam.common.core.utils.ThreadLocalUtil;
import com.liam.friend.domain.exam.dto.ExamQueryDTO;
import com.liam.friend.domain.exam.dto.ExamRankDTO;
import com.liam.friend.domain.exam.vo.ExamRankVO;
import com.liam.friend.domain.exam.vo.ExamVO;
import com.liam.friend.domain.user.vo.UserVO;
import com.liam.friend.manager.ExamCacheManager;
import com.liam.friend.manager.UserCacheManager;
import com.liam.friend.mapper.exam.ExamMapper;
import com.liam.friend.mapper.user.UserExamMapper;
import com.liam.friend.service.exam.IExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: LiamLMK
 * @CreateTime: 2025-04-26
 * @Description:
 * @Version: 1.0
 */

@Service
public class ExamServiceImpl implements IExamService {

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private ExamCacheManager examCacheManager;

    @Autowired
    private UserExamMapper userExamMapper;

    @Autowired
    private UserCacheManager userCacheManager;


    @Override
    public List<ExamVO> list(ExamQueryDTO examQueryDTO) {
        PageHelper.startPage(examQueryDTO.getPageNum(), examQueryDTO.getPageSize());
        return examMapper.selectExamList(examQueryDTO);
    }

    @Override
    public TableDataInfo redisList(ExamQueryDTO examQueryDTO) {
        // 从redis当中获取竞赛列表数据
        Long total = examCacheManager.getListSize(examQueryDTO.getType(), null);
        List<ExamVO> examVOlist;
        if(total == null || total <= 0) {
            // 从数据库中查询
            examVOlist = list(examQueryDTO);
            // 更新到redis
            examCacheManager.refreshCache(examQueryDTO.getType(), null);
            total = new PageInfo<>(examVOlist).getTotal();
        } else {
            examVOlist = examCacheManager.getExamVOList(examQueryDTO, null);
            total = examCacheManager.getListSize(examQueryDTO.getType(), null);
        }
        if(CollectionUtil.isEmpty(examVOlist)) {
            return TableDataInfo.empty();
        }
        assembleExamVOList(examVOlist);
        return TableDataInfo.success(examVOlist, total);
    }

    @Override
    public TableDataInfo rankList(ExamRankDTO examRankDTO) {
        // 从redis当中获取竞赛列表数据
        Long total = examCacheManager.getRankListSize(examRankDTO.getExamId());
        List<ExamRankVO> examRankVOlist;
        if(total == null || total <= 0) {
            PageHelper.startPage(examRankDTO.getPageNum(), examRankDTO.getPageSize());
            // 从数据库中查询
            examRankVOlist = userExamMapper.selectExamRankList(examRankDTO.getExamId());
            // 更新到redis
            examCacheManager.refreshExamRankCache(examRankDTO.getExamId());
            total = new PageInfo<>(examRankVOlist).getTotal();
        } else {
            examRankVOlist = examCacheManager.getExamRankList(examRankDTO);
        }
        if(CollectionUtil.isEmpty(examRankVOlist)) {
            return TableDataInfo.empty();
        }
        assembleExamRankVOList(examRankVOlist);
        return TableDataInfo.success(examRankVOlist, total);
    }

    private void assembleExamRankVOList(List<ExamRankVO> examRankVOlist) {
        if(CollectionUtil.isEmpty(examRankVOlist)) {
            return;
        }
        for (ExamRankVO examRankVO : examRankVOlist) {
            Long userId = examRankVO.getUserId();
            UserVO user = userCacheManager.getUserById(userId);
            examRankVO.setNickName(user.getNickName());
        }
    }

    @Override
    public String getFirstQuestion(Long examId) {
        Long listSize = examCacheManager.getExamQuestionListSize(examId);
        if(listSize == null || listSize == 0) {
            examCacheManager.refreshExamQuestionCache(examId);
        }
        return examCacheManager.getFirstQuestion(examId).toString();
    }

    @Override
    public String preQuestion(Long examId, Long questionId) {
        checkAndRefresh(examId);
        return examCacheManager.preQuestion(examId, questionId).toString();
    }

    @Override
    public String nextQuestion(Long examId, Long questionId) {
        checkAndRefresh(examId);
        return examCacheManager.nextQuestion(examId, questionId).toString();
    }

    private void checkAndRefresh(Long examId) {
        Long listSize = examCacheManager.getExamQuestionListSize(examId);
        if (listSize == null || listSize <= 0) {
            examCacheManager.refreshExamQuestionCache(examId);
        }
    }

    private void assembleExamVOList(List<ExamVO> examVOList) {
        Long userId = ThreadLocalUtil.get(Constants.USER_ID, Long.class);
        List<Long> userExamIdList = examCacheManager.getAllUserExamList(userId);
        if (CollectionUtil.isEmpty(userExamIdList)) {
            return;
        }
        for (ExamVO examVO : examVOList) {
            if (userExamIdList.contains(examVO.getExamId())) {
                examVO.setEnter(true);
            }
        }
    }
}
